﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserAuditSections : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ds.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(ds_Select);
        if (!IsPostBack)
        {
            if (Request.QueryString["Id"] != null)
            {
                try
                {
                    AuditId = int.Parse(Request.QueryString["Id"]);
                }
                catch (FormatException)
                {
                }
            }
            grid.MasterTableView.Columns.OfType<Telerik.Web.UI.GridHyperLinkColumn>().Where(x => x.UniqueName == "View").Single().DataNavigateUrlFormatString = ResolveCustomControlPath("~/UserAnswerItems.ascx?Id={0}");
        }
    }

    void ds_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        CarbonExchange.Bll.UserSectionFactory factory = new CarbonExchange.Bll.UserSectionFactory();
        e.SetData(factory.GetAllByAuditId(AuditId));
    }

    public int AuditId { get; set; }

    protected override object SaveViewState()
    {
        ViewState["AuidtId"] = AuditId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        AuditId = (int)ViewState["AuidtId"];
    }

    public override void GetExtaBreadCrumbs(Dictionary<string, string> extaItems)
    {
        if (AuditId > 0)
        {
            CarbonExchange.Bll.AuditFactory auditFactory = new CarbonExchange.Bll.AuditFactory();
            var audit = auditFactory.GetById(AuditId);
            if (audit != null)
            { 
                var user = Acms.Bll.User.GetById(audit.UserId);
                if(user != null)
                {
                    extaItems.Add(user.DisplayName, ResolveCustomControlPath(string.Format("~/UserAuditList.ascx?Id={0}", user.Id)));
                    extaItems.Add(audit.EndDate.ToString("D"), ResolveCustomControlPath(string.Format("~/UserAuditSections.ascx?Id={0}", audit.Id)));
                }
            }

        }
        base.GetExtaBreadCrumbs(extaItems);
    }
}
